<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>JavaScript中的一些实用的工具函数 | 孙志豪的博客</title><meta name="author" content="孙志豪"><meta name="copyright" content="孙志豪"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="又到周五啦，快来一起摸鱼呀！今天来看看JavaScript中的一些实用的工具函数，希望能帮助你提高开发效率！整理不易，如果觉得有用就点个赞吧！  1. 数字操作（1）生成指定范围随机数12export const randomNum &#x3D; (min, max) &#x3D;&gt; Math.floor(Math.random() * (max - min + 1)) + min;  （2）数字千分位分隔1">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript中的一些实用的工具函数">
<meta property="og:url" content="https://szhihao.gitee.io/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E5%B8%B8%E7%94%A8%E7%9A%84%E5%87%BD%E6%95%B0%E5%B0%81%E8%A3%85/index.html">
<meta property="og:site_name" content="孙志豪的博客">
<meta property="og:description" content="又到周五啦，快来一起摸鱼呀！今天来看看JavaScript中的一些实用的工具函数，希望能帮助你提高开发效率！整理不易，如果觉得有用就点个赞吧！  1. 数字操作（1）生成指定范围随机数12export const randomNum &#x3D; (min, max) &#x3D;&gt; Math.floor(Math.random() * (max - min + 1)) + min;  （2）数字千分位分隔1">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://szhihao.gitee.io/img/%E5%A4%A9%E7%A9%BA.jpeg">
<meta property="article:published_time" content="2022-10-11T12:01:36.000Z">
<meta property="article:modified_time" content="2022-10-11T09:29:23.118Z">
<meta property="article:author" content="孙志豪">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://szhihao.gitee.io/img/%E5%A4%A9%E7%A9%BA.jpeg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://szhihao.gitee.io/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E5%B8%B8%E7%94%A8%E7%9A%84%E5%87%BD%E6%95%B0%E5%B0%81%E8%A3%85/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","languages":{"hits_empty":"We didn't find any results for the search: ${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'JavaScript中的一些实用的工具函数',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-10-11 17:29:23'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/%E5%A4%B4%E5%83%8F.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">44</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">15</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fa fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 影视</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/links/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/%E5%A4%A9%E7%A9%BA.jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">孙志豪的博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> Search</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fa fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 影视</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/links/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">JavaScript中的一些实用的工具函数</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2022-10-11T09:29:23.118Z" title="Updated 2022-10-11 17:29:23">2022-10-11</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/">核心js（手写篇）</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="JavaScript中的一些实用的工具函数"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><hr>
<p>又到周五啦，快来一起摸鱼呀！今天来看看JavaScript中的一些实用的工具函数，希望能帮助你提高开发效率！整理不易，如果觉得有用就点个赞吧！</p>
<p><img src= "" data-lazy-src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d8bb008bdbe44f199e9bd7c37cec6604~tplv-k3u1fbpfcp-watermark.awebp" alt="实用工具函数.png"></p>
<h3 id="1-数字操作"><a href="#1-数字操作" class="headerlink" title="1. 数字操作"></a>1. 数字操作</h3><h4 id="（1）生成指定范围随机数"><a href="#（1）生成指定范围随机数" class="headerlink" title="（1）生成指定范围随机数"></a>（1）生成指定范围随机数</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">randomNum</span> = (<span class="params">min, max</span>) =&gt; <span class="title class_">Math</span>.<span class="title function_">floor</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * (max - min + <span class="number">1</span>)) + min;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（2）数字千分位分隔"><a href="#（2）数字千分位分隔" class="headerlink" title="（2）数字千分位分隔"></a>（2）数字千分位分隔</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">format</span> = (<span class="params">n</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> num = n.<span class="title function_">toString</span>();</span><br><span class="line">    <span class="keyword">let</span> len = num.<span class="property">length</span>;</span><br><span class="line">    <span class="keyword">if</span> (len &lt;= <span class="number">3</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> num;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> temp = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">        <span class="keyword">let</span> remainder = len % <span class="number">3</span>;</span><br><span class="line">        <span class="keyword">if</span> (remainder &gt; <span class="number">0</span>) &#123; <span class="comment">// 不是3的整数倍</span></span><br><span class="line">            <span class="keyword">return</span> num.<span class="title function_">slice</span>(<span class="number">0</span>, remainder) + <span class="string">&#x27;,&#x27;</span> + num.<span class="title function_">slice</span>(remainder, len).<span class="title function_">match</span>(<span class="regexp">/\d&#123;3&#125;/g</span>).<span class="title function_">join</span>(<span class="string">&#x27;,&#x27;</span>) + temp;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123; <span class="comment">// 3的整数倍</span></span><br><span class="line">            <span class="keyword">return</span> num.<span class="title function_">slice</span>(<span class="number">0</span>, len).<span class="title function_">match</span>(<span class="regexp">/\d&#123;3&#125;/g</span>).<span class="title function_">join</span>(<span class="string">&#x27;,&#x27;</span>) + temp; </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="2-数组操作"><a href="#2-数组操作" class="headerlink" title="2. 数组操作"></a>2. 数组操作</h3><h4 id="（1）数组乱序"><a href="#（1）数组乱序" class="headerlink" title="（1）数组乱序"></a>（1）数组乱序</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">arrScrambling</span> = (<span class="params">arr</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.<span class="property">length</span>; i++) &#123;</span><br><span class="line">      <span class="keyword">const</span> randomIndex = <span class="title class_">Math</span>.<span class="title function_">round</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * (arr.<span class="property">length</span> - <span class="number">1</span> - i)) + i;</span><br><span class="line">      [arr[i], arr[randomIndex]] = [arr[randomIndex], arr[i]];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（2）数组扁平化"><a href="#（2）数组扁平化" class="headerlink" title="（2）数组扁平化"></a>（2）数组扁平化</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">flatten</span> = (<span class="params">arr</span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">let</span> result = [];</span><br><span class="line">  <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.<span class="property">length</span>; i++) &#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="title class_">Array</span>.<span class="title function_">isArray</span>(arr[i])) &#123;</span><br><span class="line">      result = result.<span class="title function_">concat</span>(<span class="title function_">flatten</span>(arr[i]));</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      result.<span class="title function_">push</span>(arr[i]);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（3）数组中获取随机数"><a href="#（3）数组中获取随机数" class="headerlink" title="（3）数组中获取随机数"></a>（3）数组中获取随机数</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">sample</span> = arr =&gt; arr[<span class="title class_">Math</span>.<span class="title function_">floor</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * arr.<span class="property">length</span>)];</span><br><span class="line"> </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="3-字符串操作"><a href="#3-字符串操作" class="headerlink" title="3. 字符串操作"></a>3. 字符串操作</h3><h4 id="（1）生成随机字符串"><a href="#（1）生成随机字符串" class="headerlink" title="（1）生成随机字符串"></a>（1）生成随机字符串</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">randomString</span> = (<span class="params">len</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> chars = <span class="string">&#x27;ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789&#x27;</span>;</span><br><span class="line">    <span class="keyword">let</span> strLen = chars.<span class="property">length</span>;</span><br><span class="line">    <span class="keyword">let</span> randomStr = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        randomStr += chars.<span class="title function_">charAt</span>(<span class="title class_">Math</span>.<span class="title function_">floor</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * strLen));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> randomStr;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（2）字符串首字母大写"><a href="#（2）字符串首字母大写" class="headerlink" title="（2）字符串首字母大写"></a>（2）字符串首字母大写</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">fistLetterUpper</span> = (<span class="params">str</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> str.<span class="title function_">charAt</span>(<span class="number">0</span>).<span class="title function_">toUpperCase</span>() + str.<span class="title function_">slice</span>(<span class="number">1</span>);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（3）手机号中间四位变成"><a href="#（3）手机号中间四位变成" class="headerlink" title="（3）手机号中间四位变成*"></a>（3）手机号中间四位变成*</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">telFormat</span> = (<span class="params">tel</span>) =&gt; &#123;</span><br><span class="line">  	tel = <span class="title class_">String</span>(tel); </span><br><span class="line">    <span class="keyword">return</span> tel.<span class="title function_">substr</span>(<span class="number">0</span>,<span class="number">3</span>) + <span class="string">&quot;****&quot;</span> + tel.<span class="title function_">substr</span>(<span class="number">7</span>);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（4）驼峰命名转换成短横线命名"><a href="#（4）驼峰命名转换成短横线命名" class="headerlink" title="（4）驼峰命名转换成短横线命名"></a>（4）驼峰命名转换成短横线命名</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">getKebabCase</span> = (<span class="params">str</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/[A-Z]/g</span>, <span class="function">(<span class="params">item</span>) =&gt;</span> <span class="string">&#x27;-&#x27;</span> + item.<span class="title function_">toLowerCase</span>()).<span class="title function_">substr</span>(<span class="number">1</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（5）短横线命名转换成驼峰命名"><a href="#（5）短横线命名转换成驼峰命名" class="headerlink" title="（5）短横线命名转换成驼峰命名"></a>（5）短横线命名转换成驼峰命名</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">getCamelCase</span> = (<span class="params">str</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> str.<span class="title function_">replace</span>( <span class="regexp">/-([a-z])/g</span>, <span class="function">(<span class="params">i, item</span>) =&gt;</span> item.<span class="title function_">toUpperCase</span>())</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（6）全角转换为半角"><a href="#（6）全角转换为半角" class="headerlink" title="（6）全角转换为半角"></a>（6）全角转换为半角</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">toCDB</span> = (<span class="params">str</span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">let</span> result = <span class="string">&quot;&quot;</span>;</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; str.<span class="property">length</span>; i++) &#123;</span><br><span class="line">    code = str.<span class="title function_">charCodeAt</span>(i);</span><br><span class="line">    <span class="keyword">if</span> (code &gt;= <span class="number">65281</span> &amp;&amp; code &lt;= <span class="number">65374</span>) &#123;</span><br><span class="line">      result += <span class="title class_">String</span>.<span class="title function_">fromCharCode</span>(str.<span class="title function_">charCodeAt</span>(i) - <span class="number">65248</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (code == <span class="number">12288</span>) &#123;</span><br><span class="line">      result += <span class="title class_">String</span>.<span class="title function_">fromCharCode</span>(str.<span class="title function_">charCodeAt</span>(i) - <span class="number">12288</span> + <span class="number">32</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      result += str.<span class="title function_">charAt</span>(i);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（7）半角转换为全角"><a href="#（7）半角转换为全角" class="headerlink" title="（7）半角转换为全角"></a>（7）半角转换为全角</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">toDBC</span> = (<span class="params">str</span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">let</span> result = <span class="string">&quot;&quot;</span>;</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; str.<span class="property">length</span>; i++) &#123;</span><br><span class="line">    code = str.<span class="title function_">charCodeAt</span>(i);</span><br><span class="line">    <span class="keyword">if</span> (code &gt;= <span class="number">33</span> &amp;&amp; code &lt;= <span class="number">126</span>) &#123;</span><br><span class="line">      result += <span class="title class_">String</span>.<span class="title function_">fromCharCode</span>(str.<span class="title function_">charCodeAt</span>(i) + <span class="number">65248</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (code == <span class="number">32</span>) &#123;</span><br><span class="line">      result += <span class="title class_">String</span>.<span class="title function_">fromCharCode</span>(str.<span class="title function_">charCodeAt</span>(i) + <span class="number">12288</span> - <span class="number">32</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      result += str.<span class="title function_">charAt</span>(i);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="4-格式转化"><a href="#4-格式转化" class="headerlink" title="4. 格式转化"></a>4. 格式转化</h3><h4 id="（1）数字转化为大写金额"><a href="#（1）数字转化为大写金额" class="headerlink" title="（1）数字转化为大写金额"></a>（1）数字转化为大写金额</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">digitUppercase</span> = (<span class="params">n</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> fraction = [<span class="string">&#x27;角&#x27;</span>, <span class="string">&#x27;分&#x27;</span>];</span><br><span class="line">    <span class="keyword">const</span> digit = [</span><br><span class="line">        <span class="string">&#x27;零&#x27;</span>, <span class="string">&#x27;壹&#x27;</span>, <span class="string">&#x27;贰&#x27;</span>, <span class="string">&#x27;叁&#x27;</span>, <span class="string">&#x27;肆&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;伍&#x27;</span>, <span class="string">&#x27;陆&#x27;</span>, <span class="string">&#x27;柒&#x27;</span>, <span class="string">&#x27;捌&#x27;</span>, <span class="string">&#x27;玖&#x27;</span></span><br><span class="line">    ];</span><br><span class="line">    <span class="keyword">const</span> unit = [</span><br><span class="line">        [<span class="string">&#x27;元&#x27;</span>, <span class="string">&#x27;万&#x27;</span>, <span class="string">&#x27;亿&#x27;</span>],</span><br><span class="line">        [<span class="string">&#x27;&#x27;</span>, <span class="string">&#x27;拾&#x27;</span>, <span class="string">&#x27;佰&#x27;</span>, <span class="string">&#x27;仟&#x27;</span>]</span><br><span class="line">    ];</span><br><span class="line">    n = <span class="title class_">Math</span>.<span class="title function_">abs</span>(n);</span><br><span class="line">    <span class="keyword">let</span> s = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; fraction.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        s += (digit[<span class="title class_">Math</span>.<span class="title function_">floor</span>(n * <span class="number">10</span> * <span class="title class_">Math</span>.<span class="title function_">pow</span>(<span class="number">10</span>, i)) % <span class="number">10</span>] + fraction[i]).<span class="title function_">replace</span>(<span class="regexp">/零./</span>, <span class="string">&#x27;&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    s = s || <span class="string">&#x27;整&#x27;</span>;</span><br><span class="line">    n = <span class="title class_">Math</span>.<span class="title function_">floor</span>(n);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; unit[<span class="number">0</span>].<span class="property">length</span> &amp;&amp; n &gt; <span class="number">0</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">let</span> p = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> j = <span class="number">0</span>; j &lt; unit[<span class="number">1</span>].<span class="property">length</span> &amp;&amp; n &gt; <span class="number">0</span>; j++) &#123;</span><br><span class="line">            p = digit[n % <span class="number">10</span>] + unit[<span class="number">1</span>][j] + p;</span><br><span class="line">            n = <span class="title class_">Math</span>.<span class="title function_">floor</span>(n / <span class="number">10</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        s = p.<span class="title function_">replace</span>(<span class="regexp">/(零.)*零$/</span>, <span class="string">&#x27;&#x27;</span>).<span class="title function_">replace</span>(<span class="regexp">/^$/</span>, <span class="string">&#x27;零&#x27;</span>) + unit[<span class="number">0</span>][i] + s;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> s.<span class="title function_">replace</span>(<span class="regexp">/(零.)*零元/</span>, <span class="string">&#x27;元&#x27;</span>)</span><br><span class="line">        .<span class="title function_">replace</span>(<span class="regexp">/(零.)+/g</span>, <span class="string">&#x27;零&#x27;</span>)</span><br><span class="line">        .<span class="title function_">replace</span>(<span class="regexp">/^整$/</span>, <span class="string">&#x27;零元整&#x27;</span>);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（2）数字转化为中文数字"><a href="#（2）数字转化为中文数字" class="headerlink" title="（2）数字转化为中文数字"></a>（2）数字转化为中文数字</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">intToChinese</span> = (<span class="params">value</span>) =&gt; &#123;</span><br><span class="line"> <span class="keyword">const</span> str = <span class="title class_">String</span>(value);</span><br><span class="line"> <span class="keyword">const</span> len = str.<span class="property">length</span>-<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">const</span> idxs = [<span class="string">&#x27;&#x27;</span>,<span class="string">&#x27;十&#x27;</span>,<span class="string">&#x27;百&#x27;</span>,<span class="string">&#x27;千&#x27;</span>,<span class="string">&#x27;万&#x27;</span>,<span class="string">&#x27;十&#x27;</span>,<span class="string">&#x27;百&#x27;</span>,<span class="string">&#x27;千&#x27;</span>,<span class="string">&#x27;亿&#x27;</span>,<span class="string">&#x27;十&#x27;</span>,<span class="string">&#x27;百&#x27;</span>,<span class="string">&#x27;千&#x27;</span>,<span class="string">&#x27;万&#x27;</span>,<span class="string">&#x27;十&#x27;</span>,<span class="string">&#x27;百&#x27;</span>,<span class="string">&#x27;千&#x27;</span>,<span class="string">&#x27;亿&#x27;</span>];</span><br><span class="line"> <span class="keyword">const</span> num = [<span class="string">&#x27;零&#x27;</span>,<span class="string">&#x27;一&#x27;</span>,<span class="string">&#x27;二&#x27;</span>,<span class="string">&#x27;三&#x27;</span>,<span class="string">&#x27;四&#x27;</span>,<span class="string">&#x27;五&#x27;</span>,<span class="string">&#x27;六&#x27;</span>,<span class="string">&#x27;七&#x27;</span>,<span class="string">&#x27;八&#x27;</span>,<span class="string">&#x27;九&#x27;</span>];</span><br><span class="line"> <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/([1-9]|0+)/g</span>, <span class="function">(<span class="params"> $, $<span class="number">1</span>, idx, full</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> pos = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span>($<span class="number">1</span>[<span class="number">0</span>] !== <span class="string">&#x27;0&#x27;</span>)&#123;</span><br><span class="line">      pos = len-idx;</span><br><span class="line">      <span class="keyword">if</span>(idx == <span class="number">0</span> &amp;&amp; $<span class="number">1</span>[<span class="number">0</span>] == <span class="number">1</span> &amp;&amp; idxs[len-idx] == <span class="string">&#x27;十&#x27;</span>)&#123;</span><br><span class="line">      	 <span class="keyword">return</span> idxs[len-idx];</span><br><span class="line">      &#125;</span><br><span class="line">     	<span class="keyword">return</span> num[$<span class="number">1</span>[<span class="number">0</span>]] + idxs[len-idx];</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">     	<span class="keyword">let</span> left = len - idx;</span><br><span class="line">     	<span class="keyword">let</span> right = len - idx + $<span class="number">1.</span>length;</span><br><span class="line">     	<span class="keyword">if</span>(<span class="title class_">Math</span>.<span class="title function_">floor</span>(right / <span class="number">4</span>) - <span class="title class_">Math</span>.<span class="title function_">floor</span>(left / <span class="number">4</span>) &gt; <span class="number">0</span>)&#123;</span><br><span class="line">      		pos = left - left % <span class="number">4</span>;</span><br><span class="line">     	&#125;</span><br><span class="line">     	<span class="keyword">if</span>( pos )&#123;</span><br><span class="line">      		<span class="keyword">return</span> idxs[pos] + num[$<span class="number">1</span>[<span class="number">0</span>]];</span><br><span class="line">     	&#125; <span class="keyword">else</span> <span class="keyword">if</span>( idx + $<span class="number">1.</span>length &gt;= len )&#123;</span><br><span class="line">      		<span class="keyword">return</span> <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">     	&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">      		<span class="keyword">return</span> num[$<span class="number">1</span>[<span class="number">0</span>]]</span><br><span class="line">     	&#125;</span><br><span class="line">    &#125;</span><br><span class="line">   &#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="5-操作存储"><a href="#5-操作存储" class="headerlink" title="5. 操作存储"></a>5. 操作存储</h3><h4 id="（1）存储loalStorage"><a href="#（1）存储loalStorage" class="headerlink" title="（1）存储loalStorage"></a>（1）存储loalStorage</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">loalStorageSet</span> = (<span class="params">key, value</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (!key) <span class="keyword">return</span>;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> value !== <span class="string">&#x27;string&#x27;</span>) &#123;</span><br><span class="line">        value = <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(value);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="variable language_">window</span>.<span class="property">localStorage</span>.<span class="title function_">setItem</span>(key, value);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（2）获取localStorage"><a href="#（2）获取localStorage" class="headerlink" title="（2）获取localStorage"></a>（2）获取localStorage</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">loalStorageGet</span> = (<span class="params">key</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (!key) <span class="keyword">return</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="variable language_">window</span>.<span class="property">localStorage</span>.<span class="title function_">getItem</span>(key);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（3）删除localStorage"><a href="#（3）删除localStorage" class="headerlink" title="（3）删除localStorage"></a>（3）删除localStorage</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">loalStorageRemove</span> = (<span class="params">key</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (!key) <span class="keyword">return</span>;</span><br><span class="line">    <span class="variable language_">window</span>.<span class="property">localStorage</span>.<span class="title function_">removeItem</span>(key);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（4）存储sessionStorage"><a href="#（4）存储sessionStorage" class="headerlink" title="（4）存储sessionStorage"></a>（4）存储sessionStorage</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">sessionStorageSet</span> = (<span class="params">key, value</span>) =&gt; &#123;</span><br><span class="line">  	<span class="keyword">if</span> (!key) <span class="keyword">return</span>;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> value !== <span class="string">&#x27;string&#x27;</span>) &#123;</span><br><span class="line">    		value = <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(value);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="variable language_">window</span>.<span class="property">sessionStorage</span>.<span class="title function_">setItem</span>(key, value)</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（5）获取sessionStorage"><a href="#（5）获取sessionStorage" class="headerlink" title="（5）获取sessionStorage"></a>（5）获取sessionStorage</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">sessionStorageGet</span> = (<span class="params">key</span>) =&gt; &#123;</span><br><span class="line">  	<span class="keyword">if</span> (!key) <span class="keyword">return</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="variable language_">window</span>.<span class="property">sessionStorage</span>.<span class="title function_">getItem</span>(key)</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（6）删除sessionStorage"><a href="#（6）删除sessionStorage" class="headerlink" title="（6）删除sessionStorage"></a>（6）删除sessionStorage</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">sessionStorageRemove</span> = (<span class="params">key</span>) =&gt; &#123;</span><br><span class="line">  	<span class="keyword">if</span> (!key) <span class="keyword">return</span>;</span><br><span class="line">    <span class="variable language_">window</span>.<span class="property">sessionStorage</span>.<span class="title function_">removeItem</span>(key)</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="6-操作cookie"><a href="#6-操作cookie" class="headerlink" title="6. 操作cookie"></a>6. 操作cookie</h3><h4 id="（1）设置cookie"><a href="#（1）设置cookie" class="headerlink" title="（1）设置cookie"></a>（1）设置cookie</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">setCookie</span> = (<span class="params">key, value, expire</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> d = <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line">    d.<span class="title function_">setDate</span>(d.<span class="title function_">getDate</span>() + expire);</span><br><span class="line">    <span class="variable language_">document</span>.<span class="property">cookie</span> = <span class="string">`<span class="subst">$&#123;key&#125;</span>=<span class="subst">$&#123;value&#125;</span>;expires=<span class="subst">$&#123;d.toUTCString()&#125;</span>`</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（2）读取cookie"><a href="#（2）读取cookie" class="headerlink" title="（2）读取cookie"></a>（2）读取cookie</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">getCookie</span> = (<span class="params">key</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> cookieStr = <span class="built_in">unescape</span>(<span class="variable language_">document</span>.<span class="property">cookie</span>);</span><br><span class="line">       <span class="keyword">const</span> arr = cookieStr.<span class="title function_">split</span>(<span class="string">&#x27;; &#x27;</span>);</span><br><span class="line">       <span class="keyword">let</span> cookieValue = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">       <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.<span class="property">length</span>; i++) &#123;</span><br><span class="line">           <span class="keyword">const</span> temp = arr[i].<span class="title function_">split</span>(<span class="string">&#x27;=&#x27;</span>);</span><br><span class="line">           <span class="keyword">if</span> (temp[<span class="number">0</span>] === key) &#123;</span><br><span class="line">               cookieValue = temp[<span class="number">1</span>];</span><br><span class="line">               <span class="keyword">break</span></span><br><span class="line">       &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> cookieValue</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（3）删除cookie"><a href="#（3）删除cookie" class="headerlink" title="（3）删除cookie"></a>（3）删除cookie</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">delCookie</span> = (<span class="params">key</span>) =&gt; &#123;</span><br><span class="line">    <span class="variable language_">document</span>.<span class="property">cookie</span> = <span class="string">`<span class="subst">$&#123;<span class="built_in">encodeURIComponent</span>(key)&#125;</span>=;expires=<span class="subst">$&#123;<span class="keyword">new</span> <span class="built_in">Date</span>()&#125;</span>`</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="7-格式校验"><a href="#7-格式校验" class="headerlink" title="7. 格式校验"></a>7. 格式校验</h3><h4 id="（1）校验身份证号码"><a href="#（1）校验身份证号码" class="headerlink" title="（1）校验身份证号码"></a>（1）校验身份证号码</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">checkCardNo</span> = (<span class="params">value</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> reg = <span class="regexp">/(^\d&#123;15&#125;$)|(^\d&#123;18&#125;$)|(^\d&#123;17&#125;(\d|X|x)$)/</span>;</span><br><span class="line">    <span class="keyword">return</span> reg.<span class="title function_">test</span>(value);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（2）校验是否包含中文"><a href="#（2）校验是否包含中文" class="headerlink" title="（2）校验是否包含中文"></a>（2）校验是否包含中文</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> haveCNChars =&gt; <span class="function">(<span class="params">value</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="regexp">/[\u4e00-\u9fa5]/</span>.<span class="title function_">test</span>(value);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（3）校验是否为中国大陆的邮政编码"><a href="#（3）校验是否为中国大陆的邮政编码" class="headerlink" title="（3）校验是否为中国大陆的邮政编码"></a>（3）校验是否为中国大陆的邮政编码</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isPostCode</span> = (<span class="params">value</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="regexp">/^[1-9][0-9]&#123;5&#125;$/</span>.<span class="title function_">test</span>(value.<span class="title function_">toString</span>());</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（4）校验是否为IPv6地址"><a href="#（4）校验是否为IPv6地址" class="headerlink" title="（4）校验是否为IPv6地址"></a>（4）校验是否为IPv6地址</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isIPv6</span> = (<span class="params">str</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Boolean</span>(str.<span class="title function_">match</span>(<span class="regexp">/:/g</span>)?str.<span class="title function_">match</span>(<span class="regexp">/:/g</span>).<span class="property">length</span>&lt;=<span class="number">7</span>:<span class="literal">false</span> &amp;&amp; <span class="regexp">/::/</span>.<span class="title function_">test</span>(str)?<span class="regexp">/^([\da-f]&#123;1,4&#125;(:|::))&#123;1,6&#125;[\da-f]&#123;1,4&#125;$/i</span>.<span class="title function_">test</span>(str):<span class="regexp">/^([\da-f]&#123;1,4&#125;:)&#123;7&#125;[\da-f]&#123;1,4&#125;$/i</span>.<span class="title function_">test</span>(str));</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（5）校验是否为邮箱地址"><a href="#（5）校验是否为邮箱地址" class="headerlink" title="（5）校验是否为邮箱地址"></a>（5）校验是否为邮箱地址</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> isEmail = (value) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="regexp">/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/</span>.<span class="title function_">test</span>(value);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（6）校验是否为中国大陆手机号"><a href="#（6）校验是否为中国大陆手机号" class="headerlink" title="（6）校验是否为中国大陆手机号"></a>（6）校验是否为中国大陆手机号</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isTel</span> = (<span class="params">value</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="regexp">/^1[3,4,5,6,7,8,9][0-9]&#123;9&#125;$/</span>.<span class="title function_">test</span>(value.<span class="title function_">toString</span>());</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（7）校验是否包含emoji表情"><a href="#（7）校验是否包含emoji表情" class="headerlink" title="（7）校验是否包含emoji表情"></a>（7）校验是否包含emoji表情</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isEmojiCharacter</span> = (<span class="params">value</span>) =&gt; &#123;</span><br><span class="line">  	value = <span class="title class_">String</span>(value);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; value.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">const</span> hs = value.<span class="title function_">charCodeAt</span>(i);</span><br><span class="line">        <span class="keyword">if</span> (<span class="number">0xd800</span> &lt;= hs &amp;&amp; hs &lt;= <span class="number">0xdbff</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (value.<span class="property">length</span> &gt; <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="keyword">const</span> ls = value.<span class="title function_">charCodeAt</span>(i + <span class="number">1</span>);</span><br><span class="line">                <span class="keyword">const</span> uc = ((hs - <span class="number">0xd800</span>) * <span class="number">0x400</span>) + (ls - <span class="number">0xdc00</span>) + <span class="number">0x10000</span>;</span><br><span class="line">                <span class="keyword">if</span> (<span class="number">0x1d000</span> &lt;= uc &amp;&amp; uc &lt;= <span class="number">0x1f77f</span>) &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (value.<span class="property">length</span> &gt; <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">const</span> ls = value.<span class="title function_">charCodeAt</span>(i + <span class="number">1</span>);</span><br><span class="line">            <span class="keyword">if</span> (ls == <span class="number">0x20e3</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="number">0x2100</span> &lt;= hs &amp;&amp; hs &lt;= <span class="number">0x27ff</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="number">0x2B05</span> &lt;= hs &amp;&amp; hs &lt;= <span class="number">0x2b07</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="number">0x2934</span> &lt;= hs &amp;&amp; hs &lt;= <span class="number">0x2935</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="number">0x3297</span> &lt;= hs &amp;&amp; hs &lt;= <span class="number">0x3299</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (hs == <span class="number">0xa9</span> || hs == <span class="number">0xae</span> || hs == <span class="number">0x303d</span> || hs == <span class="number">0x3030</span></span><br><span class="line">                    || hs == <span class="number">0x2b55</span> || hs == <span class="number">0x2b1c</span> || hs == <span class="number">0x2b1b</span></span><br><span class="line">                    || hs == <span class="number">0x2b50</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  	<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="8-操作URL"><a href="#8-操作URL" class="headerlink" title="8. 操作URL"></a>8. 操作URL</h3><h4 id="（1）获取URL参数列表"><a href="#（1）获取URL参数列表" class="headerlink" title="（1）获取URL参数列表"></a>（1）获取URL参数列表</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">GetRequest</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> url = location.<span class="property">search</span>;</span><br><span class="line">    <span class="keyword">const</span> paramsStr = <span class="regexp">/.+\?(.+)$/</span>.<span class="title function_">exec</span>(url)[<span class="number">1</span>]; <span class="comment">// 将 ? 后面的字符串取出来</span></span><br><span class="line">    <span class="keyword">const</span> paramsArr = paramsStr.<span class="title function_">split</span>(<span class="string">&#x27;&amp;&#x27;</span>); <span class="comment">// 将字符串以 &amp; 分割后存到数组中</span></span><br><span class="line">    <span class="keyword">let</span> paramsObj = &#123;&#125;;</span><br><span class="line">    <span class="comment">// 将 params 存到对象中</span></span><br><span class="line">    paramsArr.<span class="title function_">forEach</span>(<span class="function"><span class="params">param</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (<span class="regexp">/=/</span>.<span class="title function_">test</span>(param)) &#123; <span class="comment">// 处理有 value 的参数</span></span><br><span class="line">        <span class="keyword">let</span> [key, val] = param.<span class="title function_">split</span>(<span class="string">&#x27;=&#x27;</span>); <span class="comment">// 分割 key 和 value</span></span><br><span class="line">        val = <span class="built_in">decodeURIComponent</span>(val); <span class="comment">// 解码</span></span><br><span class="line">        val = <span class="regexp">/^\d+$/</span>.<span class="title function_">test</span>(val) ? <span class="built_in">parseFloat</span>(val) : val; <span class="comment">// 判断是否转为数字</span></span><br><span class="line">        <span class="keyword">if</span> (paramsObj.<span class="title function_">hasOwnProperty</span>(key)) &#123; <span class="comment">// 如果对象有 key，则添加一个值</span></span><br><span class="line">          paramsObj[key] = [].<span class="title function_">concat</span>(paramsObj[key], val);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123; <span class="comment">// 如果对象没有这个 key，创建 key 并设置值</span></span><br><span class="line">          paramsObj[key] = val;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123; <span class="comment">// 处理没有 value 的参数</span></span><br><span class="line">        paramsObj[param] = <span class="literal">true</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">return</span> paramsObj;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（2）检测URL是否有效"><a href="#（2）检测URL是否有效" class="headerlink" title="（2）检测URL是否有效"></a>（2）检测URL是否有效</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">getUrlState</span> = (<span class="params">URL</span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">let</span> xmlhttp = <span class="keyword">new</span> <span class="title class_">ActiveXObject</span>(<span class="string">&quot;microsoft.xmlhttp&quot;</span>);</span><br><span class="line">  xmlhttp.<span class="title class_">Open</span>(<span class="string">&quot;GET&quot;</span>, <span class="variable constant_">URL</span>, <span class="literal">false</span>);</span><br><span class="line">  <span class="keyword">try</span> &#123;</span><br><span class="line">    xmlhttp.<span class="title class_">Send</span>();</span><br><span class="line">  &#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">  &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> result = xmlhttp.<span class="property">responseText</span>;</span><br><span class="line">    <span class="keyword">if</span> (result) &#123;</span><br><span class="line">      <span class="keyword">if</span> (xmlhttp.<span class="property">Status</span> == <span class="number">200</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（3）键值对拼接成URL参数"><a href="#（3）键值对拼接成URL参数" class="headerlink" title="（3）键值对拼接成URL参数"></a>（3）键值对拼接成URL参数</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">params2Url</span> = (<span class="params">obj</span>) =&gt; &#123;</span><br><span class="line">     <span class="keyword">let</span> params = []</span><br><span class="line">     <span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">in</span> obj) &#123;</span><br><span class="line">       params.<span class="title function_">push</span>(<span class="string">`<span class="subst">$&#123;key&#125;</span>=<span class="subst">$&#123;obj[key]&#125;</span>`</span>);</span><br><span class="line">     &#125;</span><br><span class="line">     <span class="keyword">return</span> <span class="built_in">encodeURIComponent</span>(params.<span class="title function_">join</span>(<span class="string">&#x27;&amp;&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（4）修改URL中的参数"><a href="#（4）修改URL中的参数" class="headerlink" title="（4）修改URL中的参数"></a>（4）修改URL中的参数</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> replaceParamVal =&gt; (paramName, replaceWith) &#123;</span><br><span class="line">   <span class="keyword">const</span> oUrl = location.<span class="property">href</span>.<span class="title function_">toString</span>();</span><br><span class="line">   <span class="keyword">const</span> re = <span class="built_in">eval</span>(<span class="string">&#x27;/(&#x27;</span>+ paramName+<span class="string">&#x27;=)([^&amp;]*)/gi&#x27;</span>);</span><br><span class="line">   location.<span class="property">href</span> = oUrl.<span class="title function_">replace</span>(re,paramName+<span class="string">&#x27;=&#x27;</span>+replaceWith);</span><br><span class="line">   <span class="keyword">return</span> location.<span class="property">href</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（5）删除URL中指定参数"><a href="#（5）删除URL中指定参数" class="headerlink" title="（5）删除URL中指定参数"></a>（5）删除URL中指定参数</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">funcUrlDel</span> = (<span class="params">name</span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">const</span> baseUrl = location.<span class="property">origin</span> + location.<span class="property">pathname</span> + <span class="string">&quot;?&quot;</span>;</span><br><span class="line">  <span class="keyword">const</span> query = location.<span class="property">search</span>.<span class="title function_">substr</span>(<span class="number">1</span>);</span><br><span class="line">  <span class="keyword">if</span> (query.<span class="title function_">indexOf</span>(name) &gt; -<span class="number">1</span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> obj = &#123;&#125;;</span><br><span class="line">    <span class="keyword">const</span> arr = query.<span class="title function_">split</span>(<span class="string">&quot;&amp;&quot;</span>);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.<span class="property">length</span>; i++) &#123;</span><br><span class="line">      arr[i] = arr[i].<span class="title function_">split</span>(<span class="string">&quot;=&quot;</span>);</span><br><span class="line">      obj[arr[i][<span class="number">0</span>]] = arr[i][<span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">delete</span> obj[name];</span><br><span class="line">    <span class="keyword">return</span> baseUrl + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(obj).<span class="title function_">replace</span>(<span class="regexp">/[\&quot;\&#123;\&#125;]/g</span>,<span class="string">&quot;&quot;</span>).<span class="title function_">replace</span>(<span class="regexp">/\:/g</span>,<span class="string">&quot;=&quot;</span>).<span class="title function_">replace</span>(<span class="regexp">/\,/g</span>,<span class="string">&quot;&amp;&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="9-设备判断"><a href="#9-设备判断" class="headerlink" title="9. 设备判断"></a>9. 设备判断</h3><h4 id="（1）判断是移动还是PC设备"><a href="#（1）判断是移动还是PC设备" class="headerlink" title="（1）判断是移动还是PC设备"></a>（1）判断是移动还是PC设备</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isMobile</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">if</span> ((navigator.<span class="property">userAgent</span>.<span class="title function_">match</span>(<span class="regexp">/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone)/i</span>))) &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">&#x27;mobile&#x27;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="string">&#x27;desktop&#x27;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（2）判断是否是苹果还是安卓移动设备"><a href="#（2）判断是否是苹果还是安卓移动设备" class="headerlink" title="（2）判断是否是苹果还是安卓移动设备"></a>（2）判断是否是苹果还是安卓移动设备</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isAppleMobileDevice</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">let</span> reg = <span class="regexp">/iphone|ipod|ipad|Macintosh/i</span>;</span><br><span class="line">  <span class="keyword">return</span> reg.<span class="title function_">test</span>(navigator.<span class="property">userAgent</span>.<span class="title function_">toLowerCase</span>());</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（3）判断是否是安卓移动设备"><a href="#（3）判断是否是安卓移动设备" class="headerlink" title="（3）判断是否是安卓移动设备"></a>（3）判断是否是安卓移动设备</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isAndroidMobileDevice</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="regexp">/android/i</span>.<span class="title function_">test</span>(navigator.<span class="property">userAgent</span>.<span class="title function_">toLowerCase</span>());</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（4）判断是Windows还是Mac系统"><a href="#（4）判断是Windows还是Mac系统" class="headerlink" title="（4）判断是Windows还是Mac系统"></a>（4）判断是Windows还是Mac系统</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">osType</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> agent = navigator.<span class="property">userAgent</span>.<span class="title function_">toLowerCase</span>();</span><br><span class="line">    <span class="keyword">const</span> isMac = <span class="regexp">/macintosh|mac os x/i</span>.<span class="title function_">test</span>(navigator.<span class="property">userAgent</span>);</span><br><span class="line">  	<span class="keyword">const</span> isWindows = agent.<span class="title function_">indexOf</span>(<span class="string">&quot;win64&quot;</span>) &gt;= <span class="number">0</span> || agent.<span class="title function_">indexOf</span>(<span class="string">&quot;wow64&quot;</span>) &gt;= <span class="number">0</span> || agent.<span class="title function_">indexOf</span>(<span class="string">&quot;win32&quot;</span>) &gt;= <span class="number">0</span> || agent.<span class="title function_">indexOf</span>(<span class="string">&quot;wow32&quot;</span>) &gt;= <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (isWindows) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;windows&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(isMac)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;mac&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（5）判断是否是微信-x2F-QQ内置浏览器"><a href="#（5）判断是否是微信-x2F-QQ内置浏览器" class="headerlink" title="（5）判断是否是微信&#x2F;QQ内置浏览器"></a>（5）判断是否是微信&#x2F;QQ内置浏览器</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">broswer</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> ua = navigator.<span class="property">userAgent</span>.<span class="title function_">toLowerCase</span>();</span><br><span class="line">    <span class="keyword">if</span> (ua.<span class="title function_">match</span>(<span class="regexp">/MicroMessenger/i</span>) == <span class="string">&quot;micromessenger&quot;</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;weixin&quot;</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (ua.<span class="title function_">match</span>(<span class="regexp">/QQ/i</span>) == <span class="string">&quot;qq&quot;</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;QQ&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（6）浏览器型号和版本"><a href="#（6）浏览器型号和版本" class="headerlink" title="（6）浏览器型号和版本"></a>（6）浏览器型号和版本</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">getExplorerInfo</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> t = navigator.<span class="property">userAgent</span>.<span class="title function_">toLowerCase</span>();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span> &lt;= t.<span class="title function_">indexOf</span>(<span class="string">&quot;msie&quot;</span>) ? &#123; <span class="comment">//ie &lt; 11</span></span><br><span class="line">        <span class="attr">type</span>: <span class="string">&quot;IE&quot;</span>,</span><br><span class="line">        <span class="attr">version</span>: <span class="title class_">Number</span>(t.<span class="title function_">match</span>(<span class="regexp">/msie ([\d]+)/</span>)[<span class="number">1</span>])</span><br><span class="line">    &#125; : !!t.<span class="title function_">match</span>(<span class="regexp">/trident\/.+?rv:(([\d.]+))/</span>) ? &#123; <span class="comment">// ie 11</span></span><br><span class="line">        <span class="attr">type</span>: <span class="string">&quot;IE&quot;</span>,</span><br><span class="line">        <span class="attr">version</span>: <span class="number">11</span></span><br><span class="line">    &#125; : <span class="number">0</span> &lt;= t.<span class="title function_">indexOf</span>(<span class="string">&quot;edge&quot;</span>) ? &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="string">&quot;Edge&quot;</span>,</span><br><span class="line">        <span class="attr">version</span>: <span class="title class_">Number</span>(t.<span class="title function_">match</span>(<span class="regexp">/edge\/([\d]+)/</span>)[<span class="number">1</span>])</span><br><span class="line">    &#125; : <span class="number">0</span> &lt;= t.<span class="title function_">indexOf</span>(<span class="string">&quot;firefox&quot;</span>) ? &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="string">&quot;Firefox&quot;</span>,</span><br><span class="line">        <span class="attr">version</span>: <span class="title class_">Number</span>(t.<span class="title function_">match</span>(<span class="regexp">/firefox\/([\d]+)/</span>)[<span class="number">1</span>])</span><br><span class="line">    &#125; : <span class="number">0</span> &lt;= t.<span class="title function_">indexOf</span>(<span class="string">&quot;chrome&quot;</span>) ? &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="string">&quot;Chrome&quot;</span>,</span><br><span class="line">        <span class="attr">version</span>: <span class="title class_">Number</span>(t.<span class="title function_">match</span>(<span class="regexp">/chrome\/([\d]+)/</span>)[<span class="number">1</span>])</span><br><span class="line">    &#125; : <span class="number">0</span> &lt;= t.<span class="title function_">indexOf</span>(<span class="string">&quot;opera&quot;</span>) ? &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="string">&quot;Opera&quot;</span>,</span><br><span class="line">        <span class="attr">version</span>: <span class="title class_">Number</span>(t.<span class="title function_">match</span>(<span class="regexp">/opera.([\d]+)/</span>)[<span class="number">1</span>])</span><br><span class="line">    &#125; : <span class="number">0</span> &lt;= t.<span class="title function_">indexOf</span>(<span class="string">&quot;Safari&quot;</span>) ? &#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="string">&quot;Safari&quot;</span>,</span><br><span class="line">        <span class="attr">version</span>: <span class="title class_">Number</span>(t.<span class="title function_">match</span>(<span class="regexp">/version\/([\d]+)/</span>)[<span class="number">1</span>])</span><br><span class="line">    &#125; : &#123;</span><br><span class="line">        <span class="attr">type</span>: t,</span><br><span class="line">        <span class="attr">version</span>: -<span class="number">1</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="10-浏览器操作"><a href="#10-浏览器操作" class="headerlink" title="10. 浏览器操作"></a>10. 浏览器操作</h3><h4 id="（1）滚动到页面顶部"><a href="#（1）滚动到页面顶部" class="headerlink" title="（1）滚动到页面顶部"></a>（1）滚动到页面顶部</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">scrollToTop</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">const</span> height = <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollTop</span> || <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollTop</span>;</span><br><span class="line">  <span class="keyword">if</span> (height &gt; <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="variable language_">window</span>.<span class="title function_">requestAnimationFrame</span>(scrollToTop);</span><br><span class="line">    <span class="variable language_">window</span>.<span class="title function_">scrollTo</span>(<span class="number">0</span>, height - height / <span class="number">8</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（2）滚动到页面底部"><a href="#（2）滚动到页面底部" class="headerlink" title="（2）滚动到页面底部"></a>（2）滚动到页面底部</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">scrollToBottom</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="variable language_">window</span>.<span class="title function_">scrollTo</span>(<span class="number">0</span>, <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">clientHeight</span>);  </span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（3）滚动到指定元素区域"><a href="#（3）滚动到指定元素区域" class="headerlink" title="（3）滚动到指定元素区域"></a>（3）滚动到指定元素区域</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">smoothScroll</span> = (<span class="params">element</span>) =&gt; &#123;</span><br><span class="line">    <span class="variable language_">document</span>.<span class="title function_">querySelector</span>(element).<span class="title function_">scrollIntoView</span>(&#123;</span><br><span class="line">        <span class="attr">behavior</span>: <span class="string">&#x27;smooth&#x27;</span></span><br><span class="line">    &#125;);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（4）获取可视窗口高度"><a href="#（4）获取可视窗口高度" class="headerlink" title="（4）获取可视窗口高度"></a>（4）获取可视窗口高度</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">getClientHeight</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> clientHeight = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">clientHeight</span> &amp;&amp; <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">clientHeight</span>) &#123;</span><br><span class="line">        clientHeight = (<span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">clientHeight</span> &lt; <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">clientHeight</span>) ? <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">clientHeight</span> : <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">clientHeight</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> &#123;</span><br><span class="line">        clientHeight = (<span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">clientHeight</span> &gt; <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">clientHeight</span>) ? <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">clientHeight</span> : <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">clientHeight</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> clientHeight;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（5）获取可视窗口宽度"><a href="#（5）获取可视窗口宽度" class="headerlink" title="（5）获取可视窗口宽度"></a>（5）获取可视窗口宽度</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">getPageViewWidth</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> (<span class="variable language_">document</span>.<span class="property">compatMode</span> == <span class="string">&quot;BackCompat&quot;</span> ? <span class="variable language_">document</span>.<span class="property">body</span> : <span class="variable language_">document</span>.<span class="property">documentElement</span>).<span class="property">clientWidth</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（6）打开浏览器全屏"><a href="#（6）打开浏览器全屏" class="headerlink" title="（6）打开浏览器全屏"></a>（6）打开浏览器全屏</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">toFullScreen</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> element = <span class="variable language_">document</span>.<span class="property">body</span>;</span><br><span class="line">    <span class="keyword">if</span> (element.<span class="property">requestFullscreen</span>) &#123;</span><br><span class="line">      element.<span class="title function_">requestFullscreen</span>()</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (element.<span class="property">mozRequestFullScreen</span>) &#123;</span><br><span class="line">      element.<span class="title function_">mozRequestFullScreen</span>()</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (element.<span class="property">msRequestFullscreen</span>) &#123;</span><br><span class="line">      element.<span class="title function_">msRequestFullscreen</span>()</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (element.<span class="property">webkitRequestFullscreen</span>) &#123;</span><br><span class="line">      element.<span class="title function_">webkitRequestFullScreen</span>()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（7）退出浏览器全屏"><a href="#（7）退出浏览器全屏" class="headerlink" title="（7）退出浏览器全屏"></a>（7）退出浏览器全屏</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">exitFullscreen</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">exitFullscreen</span>) &#123;</span><br><span class="line">      <span class="variable language_">document</span>.<span class="title function_">exitFullscreen</span>()</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">msExitFullscreen</span>) &#123;</span><br><span class="line">      <span class="variable language_">document</span>.<span class="title function_">msExitFullscreen</span>()</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">mozCancelFullScreen</span>) &#123;</span><br><span class="line">      <span class="variable language_">document</span>.<span class="title function_">mozCancelFullScreen</span>()</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">webkitExitFullscreen</span>) &#123;</span><br><span class="line">      <span class="variable language_">document</span>.<span class="title function_">webkitExitFullscreen</span>()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="11-时间操作"><a href="#11-时间操作" class="headerlink" title="11. 时间操作"></a>11. 时间操作</h3><h4 id="（1）当前时间"><a href="#（1）当前时间" class="headerlink" title="（1）当前时间"></a>（1）当前时间</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">nowTime</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> now = <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line">    <span class="keyword">const</span> year = now.<span class="title function_">getFullYear</span>();</span><br><span class="line">    <span class="keyword">const</span> month = now.<span class="title function_">getMonth</span>();</span><br><span class="line">    <span class="keyword">const</span> date = now.<span class="title function_">getDate</span>() &gt;= <span class="number">10</span> ? now.<span class="title function_">getDate</span>() : (<span class="string">&#x27;0&#x27;</span> + now.<span class="title function_">getDate</span>());</span><br><span class="line">    <span class="keyword">const</span> hour = now.<span class="title function_">getHours</span>() &gt;= <span class="number">10</span> ? now.<span class="title function_">getHours</span>() : (<span class="string">&#x27;0&#x27;</span> + now.<span class="title function_">getHours</span>());</span><br><span class="line">    <span class="keyword">const</span> miu = now.<span class="title function_">getMinutes</span>() &gt;= <span class="number">10</span> ? now.<span class="title function_">getMinutes</span>() : (<span class="string">&#x27;0&#x27;</span> + now.<span class="title function_">getMinutes</span>());</span><br><span class="line">    <span class="keyword">const</span> sec = now.<span class="title function_">getSeconds</span>() &gt;= <span class="number">10</span> ? now.<span class="title function_">getSeconds</span>() : (<span class="string">&#x27;0&#x27;</span> + now.<span class="title function_">getSeconds</span>());</span><br><span class="line">    <span class="keyword">return</span> +year + <span class="string">&quot;年&quot;</span> + (month + <span class="number">1</span>) + <span class="string">&quot;月&quot;</span> + date + <span class="string">&quot;日 &quot;</span> + hour + <span class="string">&quot;:&quot;</span> + miu + <span class="string">&quot;:&quot;</span> + sec;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（2）格式化时间"><a href="#（2）格式化时间" class="headerlink" title="（2）格式化时间"></a>（2）格式化时间</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">dateFormater</span> = (<span class="params">formater, time</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> date = time ? <span class="keyword">new</span> <span class="title class_">Date</span>(time) : <span class="keyword">new</span> <span class="title class_">Date</span>(),</span><br><span class="line">        Y = date.<span class="title function_">getFullYear</span>() + <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        M = date.<span class="title function_">getMonth</span>() + <span class="number">1</span>,</span><br><span class="line">        D = date.<span class="title function_">getDate</span>(),</span><br><span class="line">        H = date.<span class="title function_">getHours</span>(),</span><br><span class="line">        m = date.<span class="title function_">getMinutes</span>(),</span><br><span class="line">        s = date.<span class="title function_">getSeconds</span>();</span><br><span class="line">    <span class="keyword">return</span> formater.<span class="title function_">replace</span>(<span class="regexp">/YYYY|yyyy/g</span>, Y)</span><br><span class="line">        .<span class="title function_">replace</span>(<span class="regexp">/YY|yy/g</span>, Y.<span class="title function_">substr</span>(<span class="number">2</span>, <span class="number">2</span>))</span><br><span class="line">        .<span class="title function_">replace</span>(<span class="regexp">/MM/g</span>,(M&lt;<span class="number">10</span> ? <span class="string">&#x27;0&#x27;</span> : <span class="string">&#x27;&#x27;</span>) + M)</span><br><span class="line">        .<span class="title function_">replace</span>(<span class="regexp">/DD/g</span>,(D&lt;<span class="number">10</span> ? <span class="string">&#x27;0&#x27;</span> : <span class="string">&#x27;&#x27;</span>) + D)</span><br><span class="line">        .<span class="title function_">replace</span>(<span class="regexp">/HH|hh/g</span>,(H&lt;<span class="number">10</span> ? <span class="string">&#x27;0&#x27;</span> : <span class="string">&#x27;&#x27;</span>) + H)</span><br><span class="line">        .<span class="title function_">replace</span>(<span class="regexp">/mm/g</span>,(m&lt;<span class="number">10</span> ? <span class="string">&#x27;0&#x27;</span> : <span class="string">&#x27;&#x27;</span>) + m)</span><br><span class="line">        .<span class="title function_">replace</span>(<span class="regexp">/ss/g</span>,(s&lt;<span class="number">10</span> ? <span class="string">&#x27;0&#x27;</span> : <span class="string">&#x27;&#x27;</span>) + s)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// dateFormater(&#x27;YYYY-MM-DD HH:mm:ss&#x27;)</span></span><br><span class="line"><span class="comment">// dateFormater(&#x27;YYYYMMDDHHmmss&#x27;)</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="12-JavaScript操作"><a href="#12-JavaScript操作" class="headerlink" title="12. JavaScript操作"></a>12. JavaScript操作</h3><h4 id="（1）阻止冒泡事件"><a href="#（1）阻止冒泡事件" class="headerlink" title="（1）阻止冒泡事件"></a>（1）阻止冒泡事件</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">stopPropagation</span> = (<span class="params">e</span>) =&gt; &#123; </span><br><span class="line">    e = e || <span class="variable language_">window</span>.<span class="property">event</span>; </span><br><span class="line">    <span class="keyword">if</span>(e.<span class="property">stopPropagation</span>) &#123;    <span class="comment">// W3C阻止冒泡方法 </span></span><br><span class="line">        e.<span class="title function_">stopPropagation</span>(); </span><br><span class="line">    &#125; <span class="keyword">else</span> &#123; </span><br><span class="line">        e.<span class="property">cancelBubble</span> = <span class="literal">true</span>; <span class="comment">// IE阻止冒泡方法 </span></span><br><span class="line">    &#125; </span><br><span class="line">&#125; </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（2）防抖函数"><a href="#（2）防抖函数" class="headerlink" title="（2）防抖函数"></a>（2）防抖函数</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">debounce</span> = (<span class="params">fn, wait</span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">let</span> timer = <span class="literal">null</span>;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> context = <span class="variable language_">this</span>,</span><br><span class="line">        args = <span class="variable language_">arguments</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (timer) &#123;</span><br><span class="line">      <span class="built_in">clearTimeout</span>(timer);</span><br><span class="line">      timer = <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    timer = <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      fn.<span class="title function_">apply</span>(context, args);</span><br><span class="line">    &#125;, wait);</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（3）节流函数"><a href="#（3）节流函数" class="headerlink" title="（3）节流函数"></a>（3）节流函数</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">throttle</span> = (<span class="params">fn, delay</span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">let</span> curTime = <span class="title class_">Date</span>.<span class="title function_">now</span>();</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> context = <span class="variable language_">this</span>,</span><br><span class="line">        args = <span class="variable language_">arguments</span>,</span><br><span class="line">        nowTime = <span class="title class_">Date</span>.<span class="title function_">now</span>();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (nowTime - curTime &gt;= delay) &#123;</span><br><span class="line">      curTime = <span class="title class_">Date</span>.<span class="title function_">now</span>();</span><br><span class="line">      <span class="keyword">return</span> fn.<span class="title function_">apply</span>(context, args);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（4）数据类型判断"><a href="#（4）数据类型判断" class="headerlink" title="（4）数据类型判断"></a>（4）数据类型判断</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">getType</span> = (<span class="params">value</span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">if</span> (value === <span class="literal">null</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> value + <span class="string">&quot;&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 判断数据是引用类型的情况</span></span><br><span class="line">  <span class="keyword">if</span> (<span class="keyword">typeof</span> value === <span class="string">&quot;object&quot;</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> valueClass = <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(value),</span><br><span class="line">      type = valueClass.<span class="title function_">split</span>(<span class="string">&quot; &quot;</span>)[<span class="number">1</span>].<span class="title function_">split</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">    type.<span class="title function_">pop</span>();</span><br><span class="line">    <span class="keyword">return</span> type.<span class="title function_">join</span>(<span class="string">&quot;&quot;</span>).<span class="title function_">toLowerCase</span>();</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="comment">// 判断数据是基本数据类型的情况和函数的情况</span></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">typeof</span> value;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（5）对象深拷贝"><a href="#（5）对象深拷贝" class="headerlink" title="（5）对象深拷贝"></a>（5）对象深拷贝</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> deepClone = (obj, hash = <span class="keyword">new</span> <span class="title class_">WeakMap</span>() =&gt; &#123;</span><br><span class="line">  <span class="comment">// 日期对象直接返回一个新的日期对象</span></span><br><span class="line">  <span class="keyword">if</span> (obj <span class="keyword">instanceof</span> <span class="title class_">Date</span>)&#123;</span><br><span class="line">  	<span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Date</span>(obj);</span><br><span class="line">  &#125; </span><br><span class="line">  <span class="comment">//正则对象直接返回一个新的正则对象     </span></span><br><span class="line">  <span class="keyword">if</span> (obj <span class="keyword">instanceof</span> <span class="title class_">RegExp</span>)&#123;</span><br><span class="line">  	<span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">RegExp</span>(obj);     </span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//如果循环引用,就用 weakMap 来解决</span></span><br><span class="line">  <span class="keyword">if</span> (hash.<span class="title function_">has</span>(obj))&#123;</span><br><span class="line">  	<span class="keyword">return</span> hash.<span class="title function_">get</span>(obj);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 获取对象所有自身属性的描述</span></span><br><span class="line">  <span class="keyword">let</span> allDesc = <span class="title class_">Object</span>.<span class="title function_">getOwnPropertyDescriptors</span>(obj);</span><br><span class="line">  <span class="comment">// 遍历传入参数所有键的特性</span></span><br><span class="line">  <span class="keyword">let</span> cloneObj = <span class="title class_">Object</span>.<span class="title function_">create</span>(<span class="title class_">Object</span>.<span class="title function_">getPrototypeOf</span>(obj), allDesc)</span><br><span class="line">  </span><br><span class="line">  hash.<span class="title function_">set</span>(obj, cloneObj)</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">of</span> <span class="title class_">Reflect</span>.<span class="title function_">ownKeys</span>(obj)) &#123; </span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> obj[key] === <span class="string">&#x27;object&#x27;</span> &amp;&amp; obj[key] !== <span class="literal">null</span>)&#123;</span><br><span class="line">    	cloneObj[key] = <span class="title function_">deepClone</span>(obj[key], hash);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    	cloneObj[key] = obj[key];</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> cloneObj</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="https://szhihao.gitee.io">孙志豪</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://szhihao.gitee.io/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E5%B8%B8%E7%94%A8%E7%9A%84%E5%87%BD%E6%95%B0%E5%B0%81%E8%A3%85/">https://szhihao.gitee.io/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E5%B8%B8%E7%94%A8%E7%9A%84%E5%87%BD%E6%95%B0%E5%B0%81%E8%A3%85/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="/img/%E5%A4%A9%E7%A9%BA.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/10/11/15-%E6%AD%A3%E5%88%99%E5%A4%A7%E5%85%A8%E7%AC%94%E8%AE%B0/%E7%9F%A5%E9%81%93%E8%BF%99%2020%20%E4%B8%AA%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%EF%BC%8C%E8%83%BD%E8%AE%A9%E4%BD%A0%E5%B0%91%E5%86%99%201,000%20%E8%A1%8C%E4%BB%A3%E7%A0%81/"><img class="prev-cover" src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">20 个正则表达式，能让你少写 1,000 行代</div></div></a></div><div class="next-post pull-right"><a href="/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E6%9C%80%E8%AF%A6%E7%BB%86%E7%9A%84%E6%89%8B%E5%86%99Promise%E6%95%99%E7%A8%8B/"><img class="next-cover" src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">史上最最最详细的手写Promise教程</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="/img/%E5%A4%B4%E5%83%8F.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">孙志豪</div><div class="author-info__description">吾日三省吾身：为人谋而不忠乎？与朋友交而不信乎？传不习乎？</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">44</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">15</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/szhihao/szhihao"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/szhihao/szhihao" target="_blank" title="Gitee"><i class="fab fa-github"></i></a><a class="social-icon" href="https://blog.csdn.net/qq_54753561" target="_blank" title="CSDN"><i class="fa fa-book-open"></i></a><a class="social-icon" href="mailto:zhihaomail@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E6%95%B0%E5%AD%97%E6%93%8D%E4%BD%9C"><span class="toc-number">1.</span> <span class="toc-text">1. 数字操作</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%881%EF%BC%89%E7%94%9F%E6%88%90%E6%8C%87%E5%AE%9A%E8%8C%83%E5%9B%B4%E9%9A%8F%E6%9C%BA%E6%95%B0"><span class="toc-number">1.1.</span> <span class="toc-text">（1）生成指定范围随机数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%882%EF%BC%89%E6%95%B0%E5%AD%97%E5%8D%83%E5%88%86%E4%BD%8D%E5%88%86%E9%9A%94"><span class="toc-number">1.2.</span> <span class="toc-text">（2）数字千分位分隔</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E6%95%B0%E7%BB%84%E6%93%8D%E4%BD%9C"><span class="toc-number">2.</span> <span class="toc-text">2. 数组操作</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%881%EF%BC%89%E6%95%B0%E7%BB%84%E4%B9%B1%E5%BA%8F"><span class="toc-number">2.1.</span> <span class="toc-text">（1）数组乱序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%882%EF%BC%89%E6%95%B0%E7%BB%84%E6%89%81%E5%B9%B3%E5%8C%96"><span class="toc-number">2.2.</span> <span class="toc-text">（2）数组扁平化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%883%EF%BC%89%E6%95%B0%E7%BB%84%E4%B8%AD%E8%8E%B7%E5%8F%96%E9%9A%8F%E6%9C%BA%E6%95%B0"><span class="toc-number">2.3.</span> <span class="toc-text">（3）数组中获取随机数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%93%8D%E4%BD%9C"><span class="toc-number">3.</span> <span class="toc-text">3. 字符串操作</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%881%EF%BC%89%E7%94%9F%E6%88%90%E9%9A%8F%E6%9C%BA%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">3.1.</span> <span class="toc-text">（1）生成随机字符串</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%882%EF%BC%89%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%A6%96%E5%AD%97%E6%AF%8D%E5%A4%A7%E5%86%99"><span class="toc-number">3.2.</span> <span class="toc-text">（2）字符串首字母大写</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%883%EF%BC%89%E6%89%8B%E6%9C%BA%E5%8F%B7%E4%B8%AD%E9%97%B4%E5%9B%9B%E4%BD%8D%E5%8F%98%E6%88%90"><span class="toc-number">3.3.</span> <span class="toc-text">（3）手机号中间四位变成*</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%884%EF%BC%89%E9%A9%BC%E5%B3%B0%E5%91%BD%E5%90%8D%E8%BD%AC%E6%8D%A2%E6%88%90%E7%9F%AD%E6%A8%AA%E7%BA%BF%E5%91%BD%E5%90%8D"><span class="toc-number">3.4.</span> <span class="toc-text">（4）驼峰命名转换成短横线命名</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%885%EF%BC%89%E7%9F%AD%E6%A8%AA%E7%BA%BF%E5%91%BD%E5%90%8D%E8%BD%AC%E6%8D%A2%E6%88%90%E9%A9%BC%E5%B3%B0%E5%91%BD%E5%90%8D"><span class="toc-number">3.5.</span> <span class="toc-text">（5）短横线命名转换成驼峰命名</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%886%EF%BC%89%E5%85%A8%E8%A7%92%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%8D%8A%E8%A7%92"><span class="toc-number">3.6.</span> <span class="toc-text">（6）全角转换为半角</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%887%EF%BC%89%E5%8D%8A%E8%A7%92%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%85%A8%E8%A7%92"><span class="toc-number">3.7.</span> <span class="toc-text">（7）半角转换为全角</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E6%A0%BC%E5%BC%8F%E8%BD%AC%E5%8C%96"><span class="toc-number">4.</span> <span class="toc-text">4. 格式转化</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%881%EF%BC%89%E6%95%B0%E5%AD%97%E8%BD%AC%E5%8C%96%E4%B8%BA%E5%A4%A7%E5%86%99%E9%87%91%E9%A2%9D"><span class="toc-number">4.1.</span> <span class="toc-text">（1）数字转化为大写金额</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%882%EF%BC%89%E6%95%B0%E5%AD%97%E8%BD%AC%E5%8C%96%E4%B8%BA%E4%B8%AD%E6%96%87%E6%95%B0%E5%AD%97"><span class="toc-number">4.2.</span> <span class="toc-text">（2）数字转化为中文数字</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E6%93%8D%E4%BD%9C%E5%AD%98%E5%82%A8"><span class="toc-number">5.</span> <span class="toc-text">5. 操作存储</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%881%EF%BC%89%E5%AD%98%E5%82%A8loalStorage"><span class="toc-number">5.1.</span> <span class="toc-text">（1）存储loalStorage</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%882%EF%BC%89%E8%8E%B7%E5%8F%96localStorage"><span class="toc-number">5.2.</span> <span class="toc-text">（2）获取localStorage</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%883%EF%BC%89%E5%88%A0%E9%99%A4localStorage"><span class="toc-number">5.3.</span> <span class="toc-text">（3）删除localStorage</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%884%EF%BC%89%E5%AD%98%E5%82%A8sessionStorage"><span class="toc-number">5.4.</span> <span class="toc-text">（4）存储sessionStorage</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%885%EF%BC%89%E8%8E%B7%E5%8F%96sessionStorage"><span class="toc-number">5.5.</span> <span class="toc-text">（5）获取sessionStorage</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%886%EF%BC%89%E5%88%A0%E9%99%A4sessionStorage"><span class="toc-number">5.6.</span> <span class="toc-text">（6）删除sessionStorage</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-%E6%93%8D%E4%BD%9Ccookie"><span class="toc-number">6.</span> <span class="toc-text">6. 操作cookie</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%881%EF%BC%89%E8%AE%BE%E7%BD%AEcookie"><span class="toc-number">6.1.</span> <span class="toc-text">（1）设置cookie</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%882%EF%BC%89%E8%AF%BB%E5%8F%96cookie"><span class="toc-number">6.2.</span> <span class="toc-text">（2）读取cookie</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%883%EF%BC%89%E5%88%A0%E9%99%A4cookie"><span class="toc-number">6.3.</span> <span class="toc-text">（3）删除cookie</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-%E6%A0%BC%E5%BC%8F%E6%A0%A1%E9%AA%8C"><span class="toc-number">7.</span> <span class="toc-text">7. 格式校验</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%881%EF%BC%89%E6%A0%A1%E9%AA%8C%E8%BA%AB%E4%BB%BD%E8%AF%81%E5%8F%B7%E7%A0%81"><span class="toc-number">7.1.</span> <span class="toc-text">（1）校验身份证号码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%882%EF%BC%89%E6%A0%A1%E9%AA%8C%E6%98%AF%E5%90%A6%E5%8C%85%E5%90%AB%E4%B8%AD%E6%96%87"><span class="toc-number">7.2.</span> <span class="toc-text">（2）校验是否包含中文</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%883%EF%BC%89%E6%A0%A1%E9%AA%8C%E6%98%AF%E5%90%A6%E4%B8%BA%E4%B8%AD%E5%9B%BD%E5%A4%A7%E9%99%86%E7%9A%84%E9%82%AE%E6%94%BF%E7%BC%96%E7%A0%81"><span class="toc-number">7.3.</span> <span class="toc-text">（3）校验是否为中国大陆的邮政编码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%884%EF%BC%89%E6%A0%A1%E9%AA%8C%E6%98%AF%E5%90%A6%E4%B8%BAIPv6%E5%9C%B0%E5%9D%80"><span class="toc-number">7.4.</span> <span class="toc-text">（4）校验是否为IPv6地址</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%885%EF%BC%89%E6%A0%A1%E9%AA%8C%E6%98%AF%E5%90%A6%E4%B8%BA%E9%82%AE%E7%AE%B1%E5%9C%B0%E5%9D%80"><span class="toc-number">7.5.</span> <span class="toc-text">（5）校验是否为邮箱地址</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%886%EF%BC%89%E6%A0%A1%E9%AA%8C%E6%98%AF%E5%90%A6%E4%B8%BA%E4%B8%AD%E5%9B%BD%E5%A4%A7%E9%99%86%E6%89%8B%E6%9C%BA%E5%8F%B7"><span class="toc-number">7.6.</span> <span class="toc-text">（6）校验是否为中国大陆手机号</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%887%EF%BC%89%E6%A0%A1%E9%AA%8C%E6%98%AF%E5%90%A6%E5%8C%85%E5%90%ABemoji%E8%A1%A8%E6%83%85"><span class="toc-number">7.7.</span> <span class="toc-text">（7）校验是否包含emoji表情</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-%E6%93%8D%E4%BD%9CURL"><span class="toc-number">8.</span> <span class="toc-text">8. 操作URL</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%881%EF%BC%89%E8%8E%B7%E5%8F%96URL%E5%8F%82%E6%95%B0%E5%88%97%E8%A1%A8"><span class="toc-number">8.1.</span> <span class="toc-text">（1）获取URL参数列表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%882%EF%BC%89%E6%A3%80%E6%B5%8BURL%E6%98%AF%E5%90%A6%E6%9C%89%E6%95%88"><span class="toc-number">8.2.</span> <span class="toc-text">（2）检测URL是否有效</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%883%EF%BC%89%E9%94%AE%E5%80%BC%E5%AF%B9%E6%8B%BC%E6%8E%A5%E6%88%90URL%E5%8F%82%E6%95%B0"><span class="toc-number">8.3.</span> <span class="toc-text">（3）键值对拼接成URL参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%884%EF%BC%89%E4%BF%AE%E6%94%B9URL%E4%B8%AD%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-number">8.4.</span> <span class="toc-text">（4）修改URL中的参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%885%EF%BC%89%E5%88%A0%E9%99%A4URL%E4%B8%AD%E6%8C%87%E5%AE%9A%E5%8F%82%E6%95%B0"><span class="toc-number">8.5.</span> <span class="toc-text">（5）删除URL中指定参数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#9-%E8%AE%BE%E5%A4%87%E5%88%A4%E6%96%AD"><span class="toc-number">9.</span> <span class="toc-text">9. 设备判断</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%881%EF%BC%89%E5%88%A4%E6%96%AD%E6%98%AF%E7%A7%BB%E5%8A%A8%E8%BF%98%E6%98%AFPC%E8%AE%BE%E5%A4%87"><span class="toc-number">9.1.</span> <span class="toc-text">（1）判断是移动还是PC设备</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%882%EF%BC%89%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E6%98%AF%E8%8B%B9%E6%9E%9C%E8%BF%98%E6%98%AF%E5%AE%89%E5%8D%93%E7%A7%BB%E5%8A%A8%E8%AE%BE%E5%A4%87"><span class="toc-number">9.2.</span> <span class="toc-text">（2）判断是否是苹果还是安卓移动设备</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%883%EF%BC%89%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E6%98%AF%E5%AE%89%E5%8D%93%E7%A7%BB%E5%8A%A8%E8%AE%BE%E5%A4%87"><span class="toc-number">9.3.</span> <span class="toc-text">（3）判断是否是安卓移动设备</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%884%EF%BC%89%E5%88%A4%E6%96%AD%E6%98%AFWindows%E8%BF%98%E6%98%AFMac%E7%B3%BB%E7%BB%9F"><span class="toc-number">9.4.</span> <span class="toc-text">（4）判断是Windows还是Mac系统</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%885%EF%BC%89%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E6%98%AF%E5%BE%AE%E4%BF%A1-x2F-QQ%E5%86%85%E7%BD%AE%E6%B5%8F%E8%A7%88%E5%99%A8"><span class="toc-number">9.5.</span> <span class="toc-text">（5）判断是否是微信&#x2F;QQ内置浏览器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%886%EF%BC%89%E6%B5%8F%E8%A7%88%E5%99%A8%E5%9E%8B%E5%8F%B7%E5%92%8C%E7%89%88%E6%9C%AC"><span class="toc-number">9.6.</span> <span class="toc-text">（6）浏览器型号和版本</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-%E6%B5%8F%E8%A7%88%E5%99%A8%E6%93%8D%E4%BD%9C"><span class="toc-number">10.</span> <span class="toc-text">10. 浏览器操作</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%881%EF%BC%89%E6%BB%9A%E5%8A%A8%E5%88%B0%E9%A1%B5%E9%9D%A2%E9%A1%B6%E9%83%A8"><span class="toc-number">10.1.</span> <span class="toc-text">（1）滚动到页面顶部</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%882%EF%BC%89%E6%BB%9A%E5%8A%A8%E5%88%B0%E9%A1%B5%E9%9D%A2%E5%BA%95%E9%83%A8"><span class="toc-number">10.2.</span> <span class="toc-text">（2）滚动到页面底部</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%883%EF%BC%89%E6%BB%9A%E5%8A%A8%E5%88%B0%E6%8C%87%E5%AE%9A%E5%85%83%E7%B4%A0%E5%8C%BA%E5%9F%9F"><span class="toc-number">10.3.</span> <span class="toc-text">（3）滚动到指定元素区域</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%884%EF%BC%89%E8%8E%B7%E5%8F%96%E5%8F%AF%E8%A7%86%E7%AA%97%E5%8F%A3%E9%AB%98%E5%BA%A6"><span class="toc-number">10.4.</span> <span class="toc-text">（4）获取可视窗口高度</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%885%EF%BC%89%E8%8E%B7%E5%8F%96%E5%8F%AF%E8%A7%86%E7%AA%97%E5%8F%A3%E5%AE%BD%E5%BA%A6"><span class="toc-number">10.5.</span> <span class="toc-text">（5）获取可视窗口宽度</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%886%EF%BC%89%E6%89%93%E5%BC%80%E6%B5%8F%E8%A7%88%E5%99%A8%E5%85%A8%E5%B1%8F"><span class="toc-number">10.6.</span> <span class="toc-text">（6）打开浏览器全屏</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%887%EF%BC%89%E9%80%80%E5%87%BA%E6%B5%8F%E8%A7%88%E5%99%A8%E5%85%A8%E5%B1%8F"><span class="toc-number">10.7.</span> <span class="toc-text">（7）退出浏览器全屏</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#11-%E6%97%B6%E9%97%B4%E6%93%8D%E4%BD%9C"><span class="toc-number">11.</span> <span class="toc-text">11. 时间操作</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%881%EF%BC%89%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4"><span class="toc-number">11.1.</span> <span class="toc-text">（1）当前时间</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%882%EF%BC%89%E6%A0%BC%E5%BC%8F%E5%8C%96%E6%97%B6%E9%97%B4"><span class="toc-number">11.2.</span> <span class="toc-text">（2）格式化时间</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#12-JavaScript%E6%93%8D%E4%BD%9C"><span class="toc-number">12.</span> <span class="toc-text">12. JavaScript操作</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%881%EF%BC%89%E9%98%BB%E6%AD%A2%E5%86%92%E6%B3%A1%E4%BA%8B%E4%BB%B6"><span class="toc-number">12.1.</span> <span class="toc-text">（1）阻止冒泡事件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%882%EF%BC%89%E9%98%B2%E6%8A%96%E5%87%BD%E6%95%B0"><span class="toc-number">12.2.</span> <span class="toc-text">（2）防抖函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%883%EF%BC%89%E8%8A%82%E6%B5%81%E5%87%BD%E6%95%B0"><span class="toc-number">12.3.</span> <span class="toc-text">（3）节流函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%884%EF%BC%89%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%88%A4%E6%96%AD"><span class="toc-number">12.4.</span> <span class="toc-text">（4）数据类型判断</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%EF%BC%885%EF%BC%89%E5%AF%B9%E8%B1%A1%E6%B7%B1%E6%8B%B7%E8%B4%9D"><span class="toc-number">12.5.</span> <span class="toc-text">（5）对象深拷贝</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/10/12/04-%E5%89%8D%E7%AB%AF%EF%BC%88%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AF%87%EF%BC%89/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AF%87/" title="性能优化面试题"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="性能优化面试题"/></a><div class="content"><a class="title" href="/2022/10/12/04-%E5%89%8D%E7%AB%AF%EF%BC%88%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AF%87%EF%BC%89/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AF%87/" title="性能优化面试题">性能优化面试题</a><time datetime="2022-10-12T12:01:36.000Z" title="Created 2022-10-12 20:01:36">2022-10-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E8%AE%B0%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS字符串方法"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS字符串方法"/></a><div class="content"><a class="title" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E8%AE%B0%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS字符串方法">JS字符串方法</a><time datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E5%AF%B9%E8%B1%A1%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E8%AE%B0%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS对象方法"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS对象方法"/></a><div class="content"><a class="title" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E5%AF%B9%E8%B1%A1%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E8%AE%B0%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS对象方法">JS对象方法</a><time datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E6%9F%A5%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS数组方法"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS数组方法"/></a><div class="content"><a class="title" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E6%9F%A5%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS数组方法">JS数组方法</a><time datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E5%BC%80%E5%8F%91%E5%B0%81%E8%A3%85%E5%87%BD%E6%95%B0/" title="开发封装函数"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="开发封装函数"/></a><div class="content"><a class="title" href="/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E5%BC%80%E5%8F%91%E5%B0%81%E8%A3%85%E5%87%BD%E6%95%B0/" title="开发封装函数">开发封装函数</a><time datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By 孙志豪</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">Search</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  Loading the Database</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="Search for Posts" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>